<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Call;
use App\Admin\Actions\GiveOther;
use App\Admin\Actions\NeedHelp;
use App\Admin\Extensions\Tools\ExcelImport;
use Dcat\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Dcat\Admin\Admin;


class BaseController extends AdminController
{
    protected $sh_status = [0 => '待审核', 1 => '通过', 2 => '驳回'];


    protected function user_id()
    {
        return Admin::user()->id;
    }

    protected function getUser()
    {
        return Admin::user();
    }

    /**
     * @param  Grid  $grid
     * @param  bool  $show            关闭显示按钮
     * @param  bool  $disableActions  关闭所有按钮
     * @param  bool  $filter          关闭筛选按钮
     * @param  bool  $create          关闭新增按钮
     * @param  bool  $edit            关闭编辑按钮
     * @param  bool  $del             关闭删除按钮
     * @return Grid
     */
    protected function gridButton(
        $grid,
        $show = true,
        $disableActions = false,
        $filter = true,
        $create = true,
        $edit = true,
        $del = true
    ) {

        $grid->tableCollapse(false);// 表格样式
        $grid->responsive();        // 手机端自适应
        $grid->model()->orderBy('id', 'desc');

        // 关闭所有按钮
        if ($disableActions) {
            $grid->disableActions();
        }

        // 查询按钮
        if ($filter) {
            $grid->disableFilterButton();
        }
        // 新增按钮
        if ($create) {
            $grid->disableCreateButton();
        }

        // 编辑按钮
        if ($edit) {
            $grid->actions(function ($actions) {
                $actions->disableEdit();
            });
        }

        // 删除权限
        if ($del) {
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });
            $grid->batchActions(function ($batch) {
                $batch->disableDelete();
            });
        }

        // 默认设置  关闭查看
        if ($show) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
        }

        return $grid;
    }


    /**
     * @param  Grid  $show
     * @param  bool  $disableListButton    关闭列表按钮
     * @param  bool disableEditButton    关闭编辑按钮
     * @param  bool  $disableDeleteButton  关闭删除按钮
     * @return Grid
     */
    protected function showButton(
        $show,
        $disableListButton = false,
        $disableEditButton = true,
        $disableDeleteButton = true
    ) {
        // 关闭列表按钮
        if ($disableListButton) {
            $show->disableListButton();
        }

        // 关闭编辑按钮
        if ($disableEditButton) {
            $show->disableEditButton();
        }

        // 关闭删除按钮
        if ($disableDeleteButton) {
            $show->disableDeleteButton();
        }

        return $show;
    }


    /**
     * @param  Grid  $from
     * @param  bool  $disableCreatingCheck  关闭断续新增按钮
     * @param  bool  $disableEditingCheck   关闭断续编辑按钮
     * @param  bool  $disableViewCheck      关闭查看按钮
     * @return Grid
     */
    protected function formButton(
        $form,
        $disableCreatingCheck = true,
        $disableEditingCheck = true,
        $disableViewCheck = true
    ) {
        // 关闭断续新增按钮
        if ($disableCreatingCheck) {
            $form->disableCreatingCheck();
        }
        // 关闭断续编辑按钮
        if ($disableEditingCheck) {
            $form->disableEditingCheck();
        }
        // 关闭查看按钮
        if ($disableViewCheck) {
            $form->disableViewCheck();
        }

        return $form;
    }


    /***
     * 设置 KEY
     * @param $grid
     */
    protected function setKey($grid)
    {
        return $grid->rows(function ($collection) {
            foreach ($collection as $k => $v) {
                $v->key = $k + 1;
            }
            return $collection;
        });
    }


    /***
     * @param          $data     要返回的数组
     * @param  int     $code     状态码
     * @param  string  $message  返回信息
     * @return \Illuminate\Http\JsonResponse
     */
    protected function returnJson($data, $code = 0, $message = '')
    {
        return response()->json([
            'code'    => $code,
            'data'    => $data,
            'message' => $message
        ]);
    }


}
